import request from '@/utils/request'
import type { AddDeptReqDto, AddReceiptReqDto, AddRoleReqDto, AddStaffReqDto, DepartmentDto, DeptDto, EditDeptReqDto, EditReceiptReqDto, EditStaffReqDto, MemberTypeDto, MenuDto, MenuReqDto, OperateRecordDto, RoleDto, RoleMenuDto, StaffDto } from './types'
import type { DictionaryDto, PageDto, PageReqDto, PagesDto } from '../types'
import type { DeptTypeCnEnum, DictionaryStatusCnEnum, MenuStatusCnEnum, PlatformTypeCnEnum, StaffInWorkStatusCnEnum } from '@/const/enum'

/**
 * 获取部门列表
 * @returns 
 */
export function getDepartmentList(): Promise<DepartmentDto[]> {
    return request.get('/crm-prod/system/dept/treeselect')
}

/**
 * 获取系统人员列表
 * @returns 
 */
export function getSystemUserList(params: PageReqDto & {
    searchText?: string
    deptId?: number | null
}): Promise<PagesDto<StaffDto>> {
    return request.get('/crm-prod/system/user/list', {
        params
    })
}

/**
 * 新增部门
 * @param data
 * @returns
 */
export function addDept(data: AddDeptReqDto): Promise<void> {
    return request.post('/crm-prod/system/dept', data)
}

/**
 * 编辑部门
 * @param data
 * @returns
 */
export function updateDept(data: EditDeptReqDto): Promise<void> {
    return request.put('/crm-prod/system/dept', data)
}

/**
 * 删除部门
 * @param data
 * @returns
 */
export function deleteDept(id: number): Promise<void> {
    return request.delete(`/crm-prod/system/dept/${id}`)
}

/**
 * 根据id获取部门详情
 * @param id 
 * @returns 
 */
export function getDeptDetail(id: number): Promise<DeptDto> {
    return request.get(`/crm-prod/system/dept/${id}`)
}

/**
 * 获取组员类型信息
 * @returns 
 */
export function getMemberType(): Promise<MemberTypeDto[]> {
    return request.get('/crm-prod/system/post/optionselect')
}

/**
 * 获取角色列表
 * @param params 
 * @returns 
 */
export function getRoleList(params: Partial<PageReqDto>): Promise<PagesDto<RoleDto>> {
    return request.get('/crm-prod/system/role/list', {
        params
    })
}

/**
 * 新建员工
 * @param data 
 * @returns 
 */
export function addStaff(data: AddStaffReqDto): Promise<void> {
    return request.post('/crm-prod/system/user', data)
}

/**
 * 根据userId获取员工信息
 * @param params 
 * @returns 
 */
export function getStaffInfo(params: {
    userId: number
}): Promise<StaffDto> {
    return request.get('/crm-prod/system/user/get-user-info', {
        params
    })
}

/**
 * 修改员工信息
 * @param data 
 * @returns 
 */
export function editStaff(data: EditStaffReqDto): Promise<void> {
    return request.put('/crm-prod/system/user', data)
}

/**
 * 查询工作信息变更记录
 * @param data 
 * @returns 
 */
export function getOperateRecordList(params: {
    userId: number
    type?: number
}): Promise<{ data: OperateRecordDto[] }> {
    return request.get('/crm-prod/system/record/operate_record-list', {
        params
    })
}

/**
 * 获取部门列表
 * @param params 
 * @returns 
 */
export function getDeptList(params: {
    platform: PlatformTypeCnEnum
    type: DeptTypeCnEnum
}): Promise<DeptDto[]> {
    return request.get('/crm-prod/system/dept/list', {
        params
    })
}

/**
 * 获取角色信息
 * @param roleId 
 * @returns 
 */
export function getRoleInfo(roleId: number): Promise<RoleDto> {
    return request.get(`/crm-prod/system/role/${roleId}`)
}

/**
 * 获取权限树形结构
 * @param roleId 
 * @param type 
 * @returns 
 */
export function getRoleMenuTree(roleId: number, type: number): Promise<{
    checkedKeys: number[]
    menus: RoleMenuDto[]
}> {
    return request.get(`/crm-prod/system/menu/roleMenuTreeSelectAll/${roleId}/${type}`)
}

/**
 * 新建角色
 * @param data 
 * @returns 
 */
export function addRole(data: AddRoleReqDto): Promise<void> {
    return request.post('/crm-prod/system/role', data)
}

/**
 * 修改角色
 * @param data 
 * @returns 
 */
export function editRole(data: AddRoleReqDto & {
    roleId: number
}): Promise<void> {
    return request.put('/crm-prod/system/role', data)
}

/**
 * 删除角色
 * @param roleId 
 * @returns 
 */
export function deleteRole(roleId: number): Promise<void> {
    return request.delete(`/crm-prod/system/role/${roleId}`)
}

/**
 * 获取字典列表
 * @param params 
 * @returns 
 */
export function getDictionaryList(params: PageReqDto & Partial<{
    dictName: string
    dictType: string
    status: DictionaryStatusCnEnum
    'params[beginTime]': string
    'params[endTime]': string
}>): Promise<PageDto<DictionaryDto>> {
    return request.get('/crm-prod/system/dict/type/list', {
        params
    })
}


/**
 * 获取菜单列表
 * @param  
 * @returns 
 */
export function getMenuList(params: {
    menuName?: string,
    status?: MenuStatusCnEnum,
    platform?: PlatformTypeCnEnum
}): Promise<MenuDto[]> {
    return request.get('/crm-prod/system/menu/list', {
        params
    })
}

/**
 * 新增菜单
 * @param data 
 * @returns 
 */
export function addMenu (data: MenuReqDto): Promise<void> {
    return request.post('/crm-prod/system/menu', data)
}

/**
 * 编辑菜单
 * @param data 
 * @returns 
 */
export function editMenu (data: MenuDto): Promise<void> {
    return request.put('/crm-prod/system/menu', data)
}

/**
 * 删除菜单
 * @param menuId 
 * @returns 
 */
export function removeMenu (menuId: number): Promise<void> {
    return request.delete(`/crm-prod/system/menu/${menuId}`)
}

/**
 * 新增收款账户
 */
export function addReceiptInfo(data: AddReceiptReqDto): Promise<void> {
    return request.post('/crm-prod/saas/note/receipt-info', data)
}


/**
 * 编辑收款账户
 */
export function editReceiptInfo(data: EditReceiptReqDto): Promise<void> {
    return request.put('/crm-prod/saas/note/receipt-info', data)
}

/**
 * 删除收款账户
 * @param id 
 * @returns 
 */
export function removeReceiptInfo(id: number): Promise<void> {
    return request.put(`/crm-prod/saas/note/receipt-info/delete?id=${id}`)
}